package com.atguigu.springcloud.alibaba.service.impl;

import com.atguigu.springcloud.alibaba.dao.OrderDao;
import com.atguigu.springcloud.alibaba.domain.Order;
import com.atguigu.springcloud.alibaba.service.AccountService;
import com.atguigu.springcloud.alibaba.service.OrderService;
import com.atguigu.springcloud.alibaba.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class OrderServiceImpl implements OrderService {
    
    private Logger logger = LoggerFactory.getLogger(OrderServiceImpl.class);

    @Resource
    private OrderDao orderDao;
    @Resource
    private AccountService accountService;
    @Resource
    private StorageService storageService;
    
    @Override
    @GlobalTransactional(name = "fsp_create_order",rollbackFor = Exception.class)
    public void create(Order order) {
        logger.info("-----> 开始新建订单");
        // 1.新建订单
        orderDao.create(order);
        logger.info("-----> 订单微服务开始调用库存，做扣减Count");

        //2 扣减库存
        storageService.decrease(order.getProductId(),order.getCount());
        logger.info("-----> 订单微服务开始调用库存，做扣减 end");
        logger.info("-----> 订单微服务开始调用账户，做扣减Money");
        // 3. 扣减账户
        accountService.decrease(order.getUserId(),order.getMoney());
        logger.info("-----> 订单微服务开始调用账户，做扣减 end");


        // 4 修改订单状态，从0到1,1 代表已经完成
        logger.info("-----> 修改订单状态开始");
        orderDao.update(order.getUserId(),0);
        logger.info("-----> 修改订单状态结束");

        logger.info("-----> 下订单结束了，O(∩_∩)O哈哈~");

    }

    @Override
    public void update(Long userId, Integer status) {

    }
}
